package designpattern.prototype.DeepClone;

import lombok.Data;

import java.io.*;
import java.util.List;

@Data
public class SpecificPrototype implements Cloneable, Serializable {

    private final String name;
    private final String sex;
    private final List<String> hobbies;

    public SpecificPrototype(String name, String sex, List<String> hobbies) {
        this.name = name;
        this.sex = sex;
        this.hobbies = hobbies;
    }

    @Override
    protected SpecificPrototype clone(){
        SpecificPrototype specificPrototype = null;
        try {
            specificPrototype = (SpecificPrototype) super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return specificPrototype;
    }

    public SpecificPrototype deepClone(){
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(stream);
            objectOutputStream.writeObject(this);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return (SpecificPrototype) objectInputStream.readObject();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String toString() {
        return "SpecificPrototype{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", hobbies='" + hobbies + '\'' +
                '}';
    }
}
